import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { message } from 'ant-design-vue';
import userConfig from '../config/user.config';
import cookie from 'js-cookie';
class Axios {
    _s: AxiosInstance;
    constructor(config: AxiosRequestConfig) {
        this._s = axios.create(config);
    }
    //泛型res--*不做数据处理*数据处理添加响应拦截
    install = () => <T = any>(conf: AxiosRequestConfig): Promise<T> => this._s.request(conf);
    getInstance = () => this._s;
}
const requestInterceptors = [
    (conf: AxiosRequestConfig) => {
        if (cookie.get(userConfig.TOKEN_KEY)) {
            conf.headers = { [userConfig.TOKEN_KEY]: cookie.get(userConfig.TOKEN_KEY) };
        }
        return conf;
    }
];
const responseInterceptors = [
    (res: AxiosResponse) => {
        if (res.data.status === 200) return res.data.data;
        if (res.data.status === 400) {
            message.warning(res.data.msg);
        } else if (res.data.status === 403) {
            message.error('抱歉，您没有该权限');
        } else if (res.data.status === 401) {
            message.error('登录过期，请退出重新登录');
        } else {
            message.error('服务器繁忙，请稍后重试');
        }
        return Promise.reject(res);
    },
    (error: any) => {
        message.error('服务器繁忙，请稍后重试');
        return Promise.reject(error);
    }
];
const axiosConfig: AxiosRequestConfig = {
    baseURL: import.meta.env.VITE_AXIOS_BASE_URL,
    method: 'post'
};
const axios1 = new Axios(axiosConfig);
axios1.getInstance().interceptors.request.use(...requestInterceptors);
axios1.getInstance().interceptors.response.use(...responseInterceptors);
export default axios1.install();